package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.util.MD5;
import com.example.model.hosp.HospitalSet;
import com.example.result.Result;
import com.example.service.HospitalSetService;
import com.example.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Description: TODO
 * @Author zx
 * @Date 2024/2/22 15:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/hosp/HospitalSet")
@Api(tags = "医院设置模块")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表中的所有信息
     *
     * @return
     */
    @ApiOperation("获取所有的医院设置信息")
    @GetMapping("findAll")
    public Result findHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.succee(list);
    }

    /**
     * 删除医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation("删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospset(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.succee();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件分页查询
     *
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation("条件分页查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> page = new Page<>(current, limit);

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hoscode = hospitalSetQueryVo.getHoscode();

        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }

        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.succee(pageHospitalSet);
    }

    /**
     * 添加医院设置
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {
            return Result.succee();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.succee(hospitalSet);
    }


    /**
     * 修改医院设置
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation("修改医院设置")
    @PostMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.succee();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院设置
     *
     * @param ids
     * @return
     */
    @DeleteMapping("batchRemove")
    @ApiOperation("批量删除医院设置")
    public Result batchRemove(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return Result.succee();
    }


    /**
     * 医院设置锁定和解锁
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable long id, @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.succee();
    }

    /**
     * 发送签名秘钥
     * @param id
     * @return
     */
    @ApiOperation("发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hosname = hospitalSet.getHosname();
        //TODO 发送短信
        return Result.succee();
    }

}
